#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (C) 2011 Plamen Valov


__all__ = ['Upattern']


class Upattern:
    _upperAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    
    _lowerAlpha = "abcdefghijklmnopqrstuvwxyz"
    
    _numerals = "0123456789"
    
    _symbols = "~!@#$%^&*()_+-=<>/?;:,."
    
    def __init__(self, length):
        self._patternBuf = self._generate(length, [self._numerals,
                                                   self._upperAlpha + self._lowerAlpha,
                                                   self._lowerAlpha + self._upperAlpha,
                                                   self._symbols + self._numerals])
    
    def __str__(self):
        return self._patternBuf
    
    @staticmethod
    def _generate(length, set_list):
        set_list_len = len(set_list)
        
        lens = []
        max_counter = 1
        for i in range(set_list_len):
            lens.append(len(set_list[i]))
            max_counter *= lens[i]
        
        length = length if length < max_counter else max_counter
        
        counter = length / set_list_len
        left_counter = length % set_list_len
        
        buf = ""
        
        q = 0
        while(q < counter):
            t = 1
            for p in range(set_list_len):
                buf += set_list[p][q/t % lens[p]]
                t *= lens[p]
            q += 1
        
        if(left_counter > 0):
            tmp_buf = ""
            t = 1
            for p in range(set_list_len):
                tmp_buf += set_list[p][q/t % lens[p]]
                t *= lens[p]
            for s in range(left_counter):
                buf += tmp_buf[s]
        
        return buf
